home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / jf93 / ASCII / MemPools / PoolTime.c < prev   
Encoding:
C/C++ Source or Header  |  1993-03-01  |  4.4 KB  |  128 lines

  1. ;/*  Pooltime.c
  2. lc -b1 -cfist -v -y -j73 pooltime
  3. blink FROM LIB:c.o pooltime.o TO pooltime LIB LIB:lc.lib LIB:amiga.lib
  4. quit
  5.  
  6. This program demonstrates the use of a memory pool to allocate memory
  7. for two timerequest structures.  Before pools, the memory required for
  8. the timerequests was allocated using AllocMem().
  9. */
  10.  
  11. /* (c)  Copyright 1993 Commodore-Amiga, Inc.   All rights reserved. */
  12. /* The information contained herein is subject to change without    */
  13. /* notice, and is provided "as is" without warranty of any kind,    */
  14. /* either expressed or implied.  The entire risk as to the use of   */
  15. /* this information is assumed by the user.                         */
  16.  
  17.  
  18. #include <exec/types.h>
  19. #include <exec/memory.h>
  20. #include <devices/timer.h>
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/alib_protos.h>
  24.  
  25. #include <stdio.h>
  26.  
  27. #ifdef LATTICE
  28. int CXBRK(void) { return(0); }     /* Disable Lattice CTRL/C handling */
  29. int chkabort(void) { return(0); }  /* really */
  30. #endif
  31.  
  32. VOID main(VOID);
  33.  
  34. void main()
  35. {
  36. struct timerequest *TimerIO[3];
  37. struct MsgPort *TimerMP;
  38. struct Message *TimerMSG;
  39. APTR *TimerPool;            /* pointer to memory pool */
  40. ULONG x,seconds[3]={4,1,2}, microseconds[3]={0,0,0};
  41. int allin = 3;
  42. char *position[]={"last","second","first"};
  43.  
  44. if (TimerMP = CreateMsgPort())
  45.     {
  46.     if (TimerIO[0] = CreateIORequest(TimerMP,sizeof(struct timerequest)) )
  47.         {
  48.         if (!OpenDevice(TIMERNAME, UNIT_VBLANK,(struct IORequest *) TimerIO[0], 0L))
  49.             {
  50.             /* Set command to TR_ADDREQUEST */
  51.             TimerIO[0]->tr_node.io_Command = TR_ADDREQUEST;
  52.  
  53.                 /* Create the memory pool */
  54.             if (TimerPool = CreatePool(MEMF_FAST,400,200))
  55.                 {
  56.                      /* Allocate pooled memory from TimerPool */
  57.                 if (TimerIO[1]=(struct timerequest *)
  58.                         AllocPooled(TimerPool,sizeof(struct timerequest)))
  59.                     {
  60.                         /* Allocate pooled memory from TimerPool */
  61.                     if (TimerIO[2]=(struct timerequest *)
  62.                             AllocPooled(TimerPool,sizeof(struct timerequest)))
  63.                         {
  64.                         /* Copy fields from request used to open the timer device */
  65.                         *TimerIO[1] = *TimerIO[0];
  66.                         *TimerIO[2] = *TimerIO[0];
  67.  
  68.                         /* Initialize other fields */
  69.                         for (x=0;x<3;x++)
  70.                             {
  71.                             TimerIO[x]->tr_time.tv_secs   = seconds[x];
  72.                             TimerIO[x]->tr_time.tv_micro  = microseconds[x];
  73.                             }
  74.  
  75.                         printf("\n\nSending multiple requests\n\n");
  76.  
  77.                         /* Send multiple requests asynchronously */
  78.                         SendIO(TimerIO[0]);
  79.                         SendIO(TimerIO[1]);
  80.                         SendIO(TimerIO[2]);
  81.  
  82.                         /* Now go to sleep with WaitPort() waiting for the requests */
  83.                         while (allin)
  84.                               {
  85.                               WaitPort(TimerMP);
  86.                               TimerMSG=GetMsg(TimerMP);  /* Get the reply message */
  87.                               for (x=0;x<3;x++)
  88.                                 if (TimerMSG==(struct Message *)TimerIO[x])
  89.                                     printf("Request %ld finished %s\n",
  90.                                            x,position[--allin]);
  91.                               }
  92.                         }
  93.                     else
  94.                         printf("Error: Could not allocate memory for TimerIO[2]\n");
  95.                     }
  96.                 else
  97.                     printf("Error: Could not allocate memory for TimerIO[1]\n");
  98.  
  99.  
  100.                 /* Delete the entire pool.  You could also free each individual
  101.                  * allocation using FreePooled(), but this is quicker because a pool's
  102.                  * puddles are automatically drained when you delete the pool.
  103.                  */
  104.                 DeletePool(TimerPool);
  105.                 }
  106.             else
  107.                 printf("Error: Could not allocate memory pool\n");
  108.  
  109.             CloseDevice(TimerIO[0]);
  110.             }
  111.         else
  112.             printf("Error: Could not open %s\n", TIMERNAME);
  113.  
  114.         DeleteIORequest(TimerIO[0]);
  115.         }
  116.     else
  117.         printf("Error: could not create IORequest\n");
  118.  
  119.     DeleteMsgPort(TimerMP);
  120.     }
  121. else
  122.     printf("Error: Could not create message port\n");
  123. }
  124.  
  125.  
  126.  
  127.  
  128. ยง